﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using SERP.DAO;
using SERP.Controllers;
namespace SERP.Modules.Organizations
{
    [System.Web.UI.ValidationProperty("ValidationText")]
    public partial class DropDownListOrganizations : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["__EVENTTARGET"] == this.ClientID)
            {
                if (SelectedIndexChanged != null)
                {
                    SelectedIndexChanged(sender, e);
                }
            }
            if (!this.IsPostBack)
            {
                txtCombo.Attributes.Add("onclick", "showCombo('dv" + this.ClientID + "',this)");
            
                BindOrganizations();
            }
        }
        public event EventHandler SelectedIndexChanged;
        protected void drd_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SelectedIndexChanged != null)
            {
                SelectedIndexChanged(sender, e);
            }
        }
        public bool AppendDataBoundItems
        {
            get
            {
                return drd.AppendDataBoundItems;
            }
            set
            {
                drd.AppendDataBoundItems = value;
            }
        }
        public string AccessKey
        {
            get
            {
                return drd.AccessKey;
            }
            set
            {
                drd.AccessKey = value;
            }
        }
        public System.Web.UI.AttributeCollection Attributes
        {
            get
            {
                return drd.Attributes;
            }
        }
        public bool AutoPostBack
        {
            get
            {
                return drd.AutoPostBack;
            }
            set
            {
                drd.AutoPostBack = value;
            }
        }
        public Color BackColor
        {
            get
            {
                return drd.BackColor;
            }
            set
            {
                drd.BackColor = value;
            }
        }
        public Color BorderColor
        {
            get
            {
                return drd.BorderColor;
            }
            set
            {
                drd.BorderColor = value;
            }
        }
        public System.Web.UI.WebControls.BorderStyle BorderStyle
        {
            get
            {
                return drd.BorderStyle;
            }
            set
            {
                drd.BorderStyle = value;
            }
        }
        public string CssClass
        {
            get
            {
                return drd.CssClass;
            }
            set
            {
                drd.CssClass = value;
            }
        }
        public string DataMember
        {
            get
            {
                return drd.DataMember;
            }
            set
            {
                drd.DataMember = value;
            }
        }
        public object DataSource
        {
            get
            {
                return drd.DataSource;
            }
            set
            {
                drd.DataSource = value;
            }
        }
        public string DataSourceID
        {
            get
            {
                return drd.DataSourceID;
            }
            set
            {
                drd.DataSourceID = value;
            }
        }
        public string DataTextField
        {
            get
            {
                return drd.DataTextField;
            }
            set
            {
                drd.DataTextField = value;
            }
        }
        public string DataTextFormatString
        {
            get
            {
                return drd.DataTextFormatString;
            }
            set
            {
                drd.DataTextFormatString = value;
            }
        }
        public string DataValueField
        {
            get
            {
                return drd.DataValueField;
            }
            set
            {
                drd.DataValueField = value;
            }
        }
        private bool _IsAddChooseItem = false;
        private bool _IsAddAllItem = false;
        private string _SelectedValue = "";
        public bool IsAddChooseItem
        {
            set
            {
                _IsAddChooseItem = value;
            }
            get
            {
                return _IsAddChooseItem;
            }
        }
        public bool IsAddAllItem
        {
            set
            {
                _IsAddAllItem = value;
            }
            get
            {
                return _IsAddAllItem;
            }
        }
        Dictionary<string, object> searchValues = new Dictionary<string, object>();
        public void AddSearchValues(string FieldName, object value)
        {
            searchValues.Add(FieldName, value);
        }
        private List<OrganizationsEntity> FilterList(List<OrganizationsEntity> ls)
        {
            if (ls == null) return new List<OrganizationsEntity>();
            if (searchValues.Count == 0) return ls;
            if (ls.Count == 0) return ls;
            System.Reflection.PropertyInfo[] fields = ls[0].GetType().GetProperties();
            return ls.FindAll(delegate(OrganizationsEntity oFind)
            {
                foreach (string key in searchValues.Keys)
                {
                    foreach (System.Reflection.PropertyInfo f in fields)
                    {
                        if (key.ToLower() == f.Name.ToLower())
                        {
                            if (f.GetValue(oFind, null).ToString().Equals(searchValues[key].ToString()))
                                return true;
                        }
                    }
                }
                return false;
            }
            );
        }
        private int parentId = -1;
        private int rootType = 0;
        public int ParentId
        {
            get
            {
                return parentId;
            }
            set
            {
                parentId = value;
            }
        }
        public int RootTyoe
        {
            get
            {
                return rootType;
            }
            set
            {
                rootType = value;
            }
        }
        private List<OrganizationsEntity> LoadData()
        {
            List<OrganizationsEntity> ls = new List<OrganizationsEntity>();
            foreach (OrganizationsEntity o in OrganizationsController.GetOrganizationByParent(parentId))
            {
                if (rootType == 0 || o.OrgType == rootType)
                {
                    OrganizationsEntity oNew = new OrganizationsEntity();

                    oNew.OrganizationId = o.OrganizationId;
                    oNew.OrgName = o.OrgName;
                    ls.Add(oNew);
                    LoadDataRescusive(ls, o.OrganizationId, "--");
                }
            }
            return ls;
        }
        private void LoadDataRescusive(List<OrganizationsEntity> ls, int parentId, string tab)
        {
            foreach (OrganizationsEntity o in OrganizationsController.GetOrganizationByParent(parentId))
            {
                OrganizationsEntity oNew = new OrganizationsEntity();
                oNew.OrganizationId = o.OrganizationId;
                oNew.OrgName = tab + o.OrgName;
                ls.Add(oNew);
                LoadDataRescusive(ls, o.OrganizationId, tab + tab);
            }
        }

        //Bind data to txtOrganizations
        public void BindOrganizations()
        {
            OrganizationsController.LoadAllOrganizations(false);
            List<OrganizationsEntity> lsOrganizations = LoadData();
            if (lsOrganizations != null)
                lsOrganizations = new List<OrganizationsEntity>(lsOrganizations);
            lsOrganizations = FilterList(lsOrganizations);
            if (IsAddChooseItem)
            {
                OrganizationsEntity eChooseItem = new OrganizationsEntity();
                eChooseItem.OrgName = Constants.CHOOSE_ITEM;
                lsOrganizations.Insert(0, eChooseItem);
            }
            if (IsAddAllItem)
            {
                OrganizationsEntity eChooseItem = new OrganizationsEntity();
                eChooseItem.OrgName = Constants.APP_ALL;
                lsOrganizations.Insert(0, eChooseItem);
            }
            this.drd.DataTextField = "OrgName";
            this.drd.SelectedValue = null;
            this.drd.DataValueField = "OrganizationId";
            this.drd.DataSource = lsOrganizations;
            this.drd.DataBind();
            if (_SelectedValue == "")
            {
                if (this.drd.Items.Count > 0)
                {
                    this.drd.SelectedIndex = 0;
                }
            }
            else
            {
                if (drd.Items.FindByValue(_SelectedValue) != null)
                {
                    this.drd.SelectedValue = _SelectedValue;
                }
                else
                {
                    if (this.drd.Items.Count > 0)
                    {
                        this.drd.SelectedIndex = 0;
                    }
                }
            }
        }
        public string SelectedValue
        {
            get
            {
                return drd.SelectedValue;
            }
            set
            {
                _SelectedValue = value;
                drd.SelectedValue = value;
            }
        }
        public int SelectedIndex
        {
            get
            {
                return drd.SelectedIndex;
            }
            set
            {
                drd.SelectedIndex = value;
            }
        }
        [System.ComponentModel.Browsable(false)]
        public string ValidationText
        {
            get
            {
                if (_IsAddChooseItem)
                {
                    if (drd.SelectedIndex <= 0)
                    {
                        return string.Empty;
                    }
                }
                else
                {
                    if (drd.SelectedIndex < 0) return string.Empty;
                }
                return drd.Text;
            }
        }
        public System.Web.UI.WebControls.Unit Width
        {
            get
            {
                return drd.Width;
            }
            set
            {
                drd.Width = value;
            }
        }
    }
}
